﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using Telerik.WinControls;

namespace QuanLyKTX
{
    using DAL;
    public partial class frm_University : Telerik.WinControls.UI.RadForm
    {

        #region Private methods
        private void BindGridTruong()
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_Truongs
                              select p;

                if (results.Count() > 0)
                    radGridView_Truong.DataSource = results;
                else radGridView_Truong.DataSource = null;

            }
        }

        private bool InsTruong(string maTruong, string tenTruong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                var results = from p in db.tbl_Truongs
                              where p.MaTruong == maTruong
                              select p;
                if (results.Count() > 0)
                {
                    MessageBox.Show("Dữ liệu đã có sẵn.");
                    return false;
                }
                else
                {
                    try
                    {
                        tbl_Truong truong = new tbl_Truong() { MaTruong = maTruong, TenTruong = tenTruong };
                        db.tbl_Truongs.InsertOnSubmit(truong);
                        db.SubmitChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
        }

        private bool UpdTruong(string maTruong, string tenTruong)
        {
            using (var db = new QuanLyKTXDataContext())
            {
                try
                {                    
                    var result = (from p in db.tbl_Truongs
                                  where p.MaTruong == maTruong
                                  select p).Single();
                    result.TenTruong = tenTruong;
                    db.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
        #endregion

        #region Events

        private void radGridView_Truong_CellValueChanged(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            UpdTruong(radGridView_Truong.CurrentRow.Cells[0].Value.ToString(), radGridView_Truong.CurrentCell.Value.ToString());
        }

        private void radButton_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }
                

        public frm_University()
        {
            InitializeComponent();
        }

        private void radButton_Luu_Click(object sender, EventArgs e)
        {
            if (!radTextBox_MaTruong.Text.Equals(string.Empty) && !radTextBox_TenTruong.Text.Equals(string.Empty))
            {
                if (InsTruong(radTextBox_MaTruong.Text.Trim(), radTextBox_TenTruong.Text.Trim()))
                    BindGridTruong();
                else
                {
                    MessageBox.Show("Lỗi khi thêm dữ liệu");
                }
            }
            else
            {
                MessageBox.Show("Mã và Tên Trường không được để trống.");
                radTextBox_MaTruong.Focus();
                radTextBox_MaTruong.SelectAll();
            }
        }

        private void frm_University_Load(object sender, EventArgs e)
        {
            BindGridTruong();
        }

        #endregion
    }
}
